<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ShipTest</title>

<link rel="stylesheet"
	href="http://code.jquery.com/qunit/qunit-1.15.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.15.0.js"></script>
<script src="../../main/js/Team.js"></script>
<script src="../../main/js/Position.js"></script>
<script src="../../main/js/Path.js"></script>
<script src="../../main/js/Maneuver.js"></script>
<script src="../../main/js/ShipAction.js"></script>
<script src="../../main/js/Ship.js"></script>

<script>
    QUnit.test("Ship properties Firespray-31", function(assert)
    {
        var ship = Ship.FIRESPRAY_31;
        var properties = Ship.properties[ship];
        assert.equal(properties.name, "Firespray-31");
        assert.equal(properties.description, "A Firespray-31.");
        assert.equal(properties.team, Team.IMPERIAL);
        assert.equal(properties.value, ship);
        assert.equal(properties.primaryFiringArc, FiringArc.FORWARD_AND_AFT);
        assert.equal(properties.shipBase, ShipBase.LARGE);
        assert.ok(properties.maneuvers);
        assert.equal(properties.maneuvers.length, 16);
        assert.ok(properties.shipActions);
        assert.equal(properties.shipActions.length, 3);
    });

    QUnit.test("Ship properties TIE Fighter", function(assert)
    {
        var ship = Ship.TIE_FIGHTER;
        var properties = Ship.properties[ship];
        assert.equal(properties.name, "TIE Fighter");
        assert.equal(properties.description, "A TIE fighter.");
        assert.equal(properties.team, Team.IMPERIAL);
        assert.equal(properties.value, ship);
        assert.equal(properties.primaryFiringArc, FiringArc.FORWARD);
        assert.equal(properties.shipBase, ShipBase.STANDARD);
        assert.ok(properties.maneuvers);
        assert.equal(properties.maneuvers.length, 16);
        assert.ok(properties.shipActions);
        assert.equal(properties.shipActions.length, 3);
    });

    QUnit.test("Ship properties X-Wing", function(assert)
    {
        var ship = Ship.X_WING;
        var properties = Ship.properties[ship];
        assert.equal(properties.name, "X-Wing");
        assert.equal(properties.description, "An X-Wing.");
        assert.equal(properties.team, Team.REBEL);
        assert.equal(properties.value, ship);
        assert.equal(properties.primaryFiringArc, FiringArc.FORWARD);
        assert.equal(properties.shipBase, ShipBase.STANDARD);
        assert.ok(properties.maneuvers);
        assert.equal(properties.maneuvers.length, 15);
        assert.ok(properties.shipActions);
        assert.equal(properties.shipActions.length, 2);
    });

    QUnit.test("FiringArc.isInFiringArc() Forward", function(assert)
    {
        var arc = FiringArc.FORWARD;
        var isInFiringArc = FiringArc.properties[arc].isInFiringArc;
        assert.ok(isInFiringArc(0));
        assert.ok(isInFiringArc(45));
        assert.ok(!isInFiringArc(46));
        assert.ok(!isInFiringArc(134));
        assert.ok(!isInFiringArc(135));
        assert.ok(!isInFiringArc(225));
        assert.ok(!isInFiringArc(226));
        assert.ok(!isInFiringArc(314));
        assert.ok(isInFiringArc(315));
    });

    QUnit.test("FiringArc.isInFiringArc() ForwardAndAft", function(assert)
    {
        var arc = FiringArc.FORWARD_AND_AFT;
        var isInFiringArc = FiringArc.properties[arc].isInFiringArc;
        assert.ok(isInFiringArc(0));
        assert.ok(isInFiringArc(45));
        assert.ok(!isInFiringArc(46));
        assert.ok(!isInFiringArc(134));
        assert.ok(isInFiringArc(135));
        assert.ok(isInFiringArc(225));
        assert.ok(!isInFiringArc(226));
        assert.ok(!isInFiringArc(314));
        assert.ok(isInFiringArc(315));
    });

    QUnit.test("ShipBase properties Large", function(assert)
    {
        var shipBase = ShipBase.LARGE;
        var properties = ShipBase.properties[shipBase];
        assert.equal(properties.width, 80);
        assert.equal(properties.height, 80);
    });

    QUnit.test("ShipBase properties Standard", function(assert)
    {
        var shipBase = ShipBase.STANDARD;
        var properties = ShipBase.properties[shipBase];
        assert.equal(properties.width, 40);
        assert.equal(properties.height, 40);
    });

    QUnit.test("ShipBase.computePolygon() Standard", function(assert)
    {
        // Setup.
        var shipBase = ShipBase.STANDARD;

        // Run.
        var result = ShipBase.computePolygon(shipBase, 0, 0, 0);

        // Verify.
        assert.ok(result);
        assert.equal(result.getPointsLength(), 10);

        var i = 0;
        var points = result.getPoints();
        assert.equal(points[i++], 20);
        assert.equal(points[i++], -20);
        assert.equal(points[i++], 20);
        assert.equal(points[i++], 20);
        assert.equal(points[i++], -20);
        assert.equal(points[i++], 20);
        assert.equal(points[i++], -20);
        assert.equal(points[i++], -20);
        assert.equal(points[i++], 20);
        assert.equal(points[i++], -20);
    });

    QUnit.test("ShipBase.computePolygon() Standard 0", function(assert)
    {
        // Setup.
        var shipBase = ShipBase.STANDARD;

        // Run.
        var result = ShipBase.computePolygon(shipBase, 10, 20, 0);

        // Verify.
        assert.ok(result);
        assert.equal(result.getPointsLength(), 10);

        var i = 0;
        var points = result.getPoints();
        assert.equal(points[i++], 30);
        assert.equal(points[i++], 0);
        assert.equal(points[i++], 30);
        assert.equal(points[i++], 40);
        assert.equal(points[i++], -10);
        assert.equal(points[i++], 40);
        assert.equal(points[i++], -10);
        assert.equal(points[i++], 0);
        assert.equal(points[i++], 30);
        assert.equal(points[i++], 0);
    });

    QUnit.test("ShipBase.computePolygon() Standard 45", function(assert)
    {
        // Setup.
        var shipBase = ShipBase.STANDARD;

        // Run.
        var result = ShipBase.computePolygon(shipBase, 10, 20, 45);

        // Verify.
        assert.ok(result);
        assert.equal(result.getPointsLength(), 10);

        var i = 0;
        var points = result.getPoints();
        assert.equal(myRound(points[i++]), 38.2843);
        assert.equal(myRound(points[i++]), 20);
        assert.equal(myRound(points[i++]), 10);
        assert.equal(myRound(points[i++]), 48.2843);
        assert.equal(myRound(points[i++]), -18.2843);
        assert.equal(myRound(points[i++]), 20);
        assert.equal(myRound(points[i++]), 10);
        assert.equal(myRound(points[i++]), -8.2843);
        assert.equal(myRound(points[i++]), 38.2843);
        assert.equal(myRound(points[i++]), 20);

        function myRound(value)
        {
            var factor = 10000;
            return Math.round(value * factor) / factor;
        }
    });
</script>
</head>
<body>
	<div id="qunit"></div>
</body>
</html>
